import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from './../../../core/api/api.service';
import { MessageService } from './../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
@Component({
    selector: 'app-user-mgmt',
    templateUrl: './user-mgmt.component.html',
})
export class UserMgmtComponent implements OnInit {
    userList = []; // 列表数据
    page: any = {pageNo: 1, pageSize: 10};
    isVisible = false; // 是否显示弹出框
    modalTitle; // 弹出框标题
    isOkLoading = false; // 弹出框按钮加载动画
    tableLoading = false;
    validateForm;
    roleList = [];
    passwordVisible = false;
    activeData: any = {};
    ngOnInit() {
        this.message.setBreadcrumb({
            breadcrumbs: [
            {name: '系统用户管理'}
        ]});
        this.getUserList();
        this.apiService.post('角色列表', {pageNo: 1, pageSize: 1000}).then((res: any) => {
            this.roleList = res.data.list;
        });
        this.validateForm = this.fb.group({
            contactName: [null, [ Validators.required]],
            contactEmail: [null, [ Validators.required]],
            name: [null, [ Validators.required]],
            contactTel: [null, [ Validators.required, Validators.pattern(/^1[0-9]{10}$/)]],
            loginId: [null, [ Validators.required]],
            loginPw: [null, [ Validators.required]],
            roleId: [null, [ Validators.required]],
            status: [null, [Validators.required]]
        });
    }
    handleOk() {
        const url = this.activeData.id ? '编辑管理员' : '创建管理员';
        if (this.validateForm.valid) {
            this.apiService.post(url, {id: this.activeData.id, ...this.validateForm.value}).then((res: any) => {
                this.isVisible = false;
                this.getUserList();
            });
        }
    }
    handleCancel() {
        this.isVisible = false;
    }
    addUser() {
        this.isVisible = true;
        this.validateForm = this.fb.group({
            contactName: [null, [ Validators.required]],
            contactEmail: [null, [ Validators.required]],
            name: [null, [ Validators.required]],
            contactTel: [null, [ Validators.required, Validators.pattern(/^1[0-9]{10}$/)]],
            loginId: [null, [ Validators.required]],
            loginPw: [null, [ Validators.required]],
            roleId: [null, [ Validators.required]],
            status: [null, [Validators.required]]
        });
        this.validateForm.reset();
    }
    updateUser(data){
        this.isVisible = true;
        this.validateForm = this.fb.group({
            contactName: [null, [ Validators.required]],
            contactEmail: [null, [ Validators.required]],
            name: [null, [ Validators.required]],
            contactTel: [null, [ Validators.required, Validators.pattern(/^1[0-9]{10}$/)]],
            loginId: [{value: null, disabled: true}, [ Validators.required]],
            roleId: [null, [ Validators.required]],
            status: [null, [Validators.required]]
        });
        this.validateForm.reset();
        this.activeData = {...data};
        this.validateForm.patchValue(this.activeData);
    }
    getUserList(params = {}) {
        this.tableLoading = true;
        this.apiService.post('管理员列表', {...params, ...this.page}).then((res: any) => {
            this.userList = res.data.list;
            this.tableLoading = false;
        }).catch(res => {
            this.tableLoading = false;
        });
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService
    ) { }
}
